<?php

	/**
	 *	ImageHandler - Uploaded image manipulation & validation class
	 *	@name ImageHandler 0.2
	 *	@author Jason Strese
	 *	@copyright 2007, Jason Strese
	 *	@version 0.2
	 */
	class ImageHandler
	{
		private $_data = null;
		private $_types = array( 'jpg', 'pjpeg', 'jpeg', 'png', 'x-png', 'gif' );
		
		/**
		 *	Image details
		 *	@since 0.1
		 */
		public $type = null;
		public $width, $height = null;
		public $size = null;
		public $true = null;
		public $tags = null;
		public $title = null;
		public $desc = null;
		public $ext = null;
		
		/**
		 *	NGen Core Object
		 *	@since 0.2
		 */
		public $core = null;
		
		/**
		 *	Type constants
		 *	@since 0.1
		 */
		const TYPE_UNK = -1;
		const TYPE_JPG = 0;
		const TYPE_PNG = 1;
		const TYPE_GIF = 2;

		/**
		 *	Original constructor
		public function __construct( $image, $tags = null, $description = '' )
		{
			$this->_data = $image;
			$this->getTags( $tags );
			$this->title = $this->hashName();
			$this->desc = $description;
		}
		*/
		
		/**
		 *	Modified constructor for NGen
		 *	@since 0.2
		 */
		public function __construct( $core, $data )
		{
			if( isset( $data['files']['image'] ) && is_uploaded_file( $data['files']['image']['tmp_name'] ) )
			{
				$this->core = &$core;
				$this->_data = $data['files']['image'];
				$this->getTags( explode( ' ', strtolower( $data['post']['tags'] ) ) );
				$this->title = $this->hashName();
				$this->desc = $data['post']['desc'];
			}
			else
			{
				$this->type = self::TYPE_UNK;
				$this->true = false;
			}
		}
		
		/**
		 *	Get validated tags
		 *	@since 0.1
		 */
		private function getTags( $tags )
		{
			if( isset( $tags[0] ) )
			{
				$this->tags = array( );
				
				foreach( $tags as $tag )
				{
					if( !isset( $tags[12] ) )
					{
						$this->tags[] = ucwords( $this->truncate( $tag, 22 ) );
					}
				}

				$this->tags = array_unique( $this->tags );
			}
		}
		
		/**
		 *	Truncates text
		 *	@since 0.1
		 */
		public function truncate( $string, $length = 12 )
		{
		    if( strlen( $string ) > $length )
			{
		       	return substr( $string, 0, $length );
		    }
			else
			{
				return $string;
		    }
		}
		
		/**
		 *	Gathers title and extension
		 *	@since 0.1
		 */
		private function splitName()
		{
			$parts = explode( '.', str_replace( ' ', '_', $this->_data['name'] ) );
			$this->title = $parts[0];
			$this->ext = strtolower( $parts[1] );
		}
		
		/**
		 *	Returns a well-formed name
		 *	@since 0.1
		 */
		private function hashName()
		{
			$this->splitName();
			return strtolower( $this->truncate( $this->title, mt_rand( 8, 10 ) ) . $this->randid() . '.' . $this->ext );
		}
				
		/**
		 *	Generates a random id number
		 *  @since 0.2
		 */
		public function randid()
		{
			return substr( md5( uniqid( mt_rand(), true ) ), 0, mt_rand( 6, 10 ) );
		}
		
		/**
		 *	Returns the type of image
		 *	@since 0.1
		 */
		public function getType()
		{
			if( $this->type === null )
			{
				switch( $this->ext )
				{
					case 'jpg':
					case 'jpeg':
						$this->type = self::TYPE_JPG;
						break;
					case 'png':
						$this->type = self::TYPE_PNG;
						break;
					case 'gif':
						$this->type = self::TYPE_GIF;
						break;
					default:
						$this->type = self::TYPE_UNK;
						break;
				}
			}
			
			return $this->type;
		}
						
		/**
		 *	Returns size in kilobytes (KB)
		 *	@since 0.1
		 */
		public function getSize()
		{
			if( $this->size === null )
			{
				$this->size = (int)$this->_data['size'] / 1024;
			}
			
			return $this->size;
		}
		
		/**
		 *	Returns dimensions of the image (height & width)
		 *	@since 0.1
		 */
		public function getDimensions()
		{
			if( $this->height === null || $this->width === null )
			{
				$size = getimagesize( $this->_data['tmp_name'] );
				$this->height = (int)$size[1];
				$this->width = (int)$size[0];
			}
			
			return array( 'height' => $this->height, 'width' => $this->width );
		}
		
		/**
		 *	Moves the uploaded file AND updates current directory
		 *	@since 0.1
		 */
		public function move()
		{
			// Server dir	/homepages/8/d211387067/htdocs/domains/mythpic/
			mkdir('/homepages/8/d211387067/htdocs/domains/mythpic/sub-isolated/assets/.storage/' . $this->title, 0755 );
						
			move_uploaded_file( $this->_data['tmp_name'],  '/homepages/8/d211387067/htdocs/domains/mythpic/sub-isolated/assets/.storage/' . $this->title . '/' . $this->title );
		}

		/**
		 *	Returns true if the image is valid, false if not
		 *	@since 0.1
		 */
		public function isTrueImage()
		{
			if( $this->true === null )
			{
				if( $this->getType() >= 0 && $this->getType() <= 2 )
				{
					$this->true = true;
				}
				else
				{
					$this->true = false;
				}
			}
			
			return $this->true;
		}
		
		/**
		 *	Return an aspect-ratio-preserved thumbnail
		 *	@since 0.1
		 */		
		public function getThumbnail($dir = './tmp/thumbs/', $width = 128, $height = 128)
		{		
			$resource = null;
			
			switch( $this->getType() )
			{
				case self::TYPE_JPG:
					$resource = imagecreatefromjpeg( $this->_data['tmp_name'] );
					break;
				case self::TYPE_PNG:
					$resource = imagecreatefrompng( $this->_data['tmp_name'] );
					break;
				case self::TYPE_GIF:
					$resource = imagecreatefromgif( $this->_data['tmp_name'] );
					break;
				default:
					trigger_error( 'ImageHandler::getThumbnail - unknown or unsupported image-type' );
					break;
			}
			
			$this->getDimensions();
			
			if( $this->width > $this->height )
			{
				$height = $this->height * ( $height / $this->width );
			}
			if( $this->height > $this->width )
			{
				$width = $this->width * ( $width / $this->height );
			}
						
			$thumbnail = imagecreatetruecolor( $width, $height );
			
			imagecopyresampled( $thumbnail, $resource, 0, 0, 0, 0, $width, $height, $this->width, $this->height );
			
			switch( $this->getType() )
			{
				case self::TYPE_JPG:
					imagejpeg( $thumbnail, $dir . $this->title );
					break;
				case self::TYPE_PNG:
					imagepng( $thumbnail, $dir . $this->title );
					break;
				case self::TYPE_GIF:
					imagegif( $thumbnail, $dir . $this->title );
					break;
				default:
					trigger_error( 'ImageHandler::getThumbnail - could not save thumbnail' );
					break;
			}
			
			imagedestroy( $thumbnail );
		}
		
		/**
		 *	Finalize this image, collect all remaining information that needs to be collected.
		 *	@since 0.1
		 */
		public function finalize()
		{
			$this->isTrueImage();
			$this->getDimensions();
			$this->getSize();
			$this->getType();
						
			$this->_data = null;
			$this->_types = null;
		}
	}
?>